﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AssetAllocationSystem.Models;
using NHibernate;
using NHibernate.Criterion;

namespace AssetAllocationSystem.Repositories
{
    public class AssetRepository : RepositoryBase<Asset>
    {
        private ISession session;
        public AssetRepository(ISession session)
            : base(session)
        {
            this.session = session;
        }

        public IList<Asset> FindByServiceKey(string serviceKey)
        {
            var assets = DetachedCriteria
                .For<Asset>()
                .Add(Restrictions.Eq("ServiceKey", serviceKey))
                .GetExecutableCriteria(session)
                .List<Asset>();
                

            return assets;
        }

        public IList<Asset> FindByConfiguration(string configuration)
        {
            var assets = DetachedCriteria
                .For<Asset>()
                .Add(Restrictions.Like("Configuration", configuration))
                .GetExecutableCriteria(session)
                .List<Asset>();

            return assets;
        }

        public IList<Asset> FindByPrimaryLocation(string primaryLocation)
        {
            var assets = DetachedCriteria
                .For<Asset>()
                .Add(Restrictions.Eq("PrimaryLocation", primaryLocation))
                .GetExecutableCriteria(session)
                .List<Asset>();

            return assets;
        }

        public IList<Asset> FindByExistingLocation(string existingLocation)
        {
            var assets = DetachedCriteria
                .For<Asset>()
                .Add(Restrictions.Eq("ExistingLocation", existingLocation))
                .GetExecutableCriteria(session)
                .List<Asset>();

            return assets;
        }

        //public Asset FindById(Guid id)
        //{
        //    ISession session = base.CreateSession();

        //    var asset = (Asset)DetachedCriteria
        //        .For<Asset>()
        //        .Add(Restrictions.Eq("Id", id))
        //        .GetExecutableCriteria(session)
        //        .UniqueResult();

        //    return asset;
        //}

        
    }
}